package com.jinyou.chengfengshop.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;


import com.jinyou.chengfengshop.Constant.Config;
import com.jinyou.chengfengshop.R;
import com.jinyou.chengfengshop.utils.CommonEvent;
import com.jinyou.chengfengshop.utils.CommonEventKey;
import com.sobot.chat.utils.ToastUtil;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import de.greenrobot.event.EventBus;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;
    //    private double pay_amout;
    public int errCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_running);
//        OurSystem.out("创建界面");
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, "wxf79b330bbebfa11c", false);
        // 将该app注册到微信
        api.registerApp("wxf79b330bbebfa11c");
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        Log.i("onReq","----------------onReq");
//    	OurSystem.out("微信支付回调结果onReq, resp.openId=" + req.openId + ",req.getType()=" + req.getType());
    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        Log.i("onResp","----------------支付成功");

        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:

                ToastUtil.showToast(WXPayEntryActivity.this, "支付成功");
                Log.i("onResp","----------------支付成功");
                EventBus.getDefault().post(new CommonEvent(CommonEventKey.PAY_SUCCESS));

                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:

                ToastUtil.showToast(WXPayEntryActivity.this, "取消支付");
                Log.i("onResp","----------------取消支付");
                EventBus.getDefault().post(new CommonEvent(CommonEventKey.PAY_CANCEL));

                //取消支付
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:

                ToastUtil.showToast(WXPayEntryActivity.this, "支付失败");
                Log.i("onResp","----------------支付失败");
                EventBus.getDefault().post(new CommonEvent(CommonEventKey.PAY_FAIL));
                //发送被拒绝
                break;
            default:
                ToastUtil.showToast(WXPayEntryActivity.this, "支付失败");
                Log.i("onResp","----------------支付失败");
                EventBus.getDefault().post(new CommonEvent(CommonEventKey.PAY_FAIL));
                //发送被拒绝
                break;
        }
        WXPayEntryActivity.this.finish();
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }

    public int getErrCode() {
        return errCode;
    }
}